﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Text;
using xiaodingmarket_application.Interfaces;
using xiaodingmarket_application.ViewModel;
using xiaodingmarket_Domain.Commands.Order;
using xiaodingmarket_Domain.Interfaces.IRepository;
using xiaodingmarket_Domain_core.Bus;
using xiaodingmarket_Infra_Data.Repository.EventStore;

namespace xiaodingmarket_application.Service
{
    public class OrderService : IOrderService
    {
        private readonly IMapper _mapper;
        private readonly IOrderRepository _orderRepository;
        private readonly IEventStoreRepository _eventStoreRepository;
        private readonly IMediatorHandler Bus;

        public OrderService(IMapper mapper,
                                  IOrderRepository orderRepository,
                                  IMediatorHandler bus,
                                  IEventStoreRepository eventStoreRepository)
        {
            _mapper = mapper;
            _orderRepository = orderRepository;
            Bus = bus;
            _eventStoreRepository = eventStoreRepository;
        }
        /// <summary>
        /// 创建,也就是卖家选择买家是调用的接口
        /// </summary>
        /// <param name="orderViewModel"></param>
        public void Create(OrderViewModel orderViewModel)
        {
            var CreateOrderCommand = _mapper.Map<CreateNewOrderCommand>(orderViewModel);
            Bus.SendCommand(CreateOrderCommand);
        }

        /// <summary>
        /// 获取某用户的大量订单信息
        /// </summary>
        /// <param name="userid">用户id</param>
        /// <param name="kind">商品or悬赏</param>
        /// <param name="Condition">订单状态</param>
        /// <param name="who">判断是买方1还是卖方0</param>
        /// <returns></returns>
        public IEnumerable<OrderViewModel> GetAll(Guid userid, int kind, int Condition,int who)
        {
            return _mapper.Map<IEnumerable<OrderViewModel>>(_orderRepository.GetAll(userid,kind,Condition,who));
        }
        /// <summary>
        /// 通过订单id获取订单详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public OrderViewModel GetById(Guid id)
        {
            return _mapper.Map<OrderViewModel>(_orderRepository.GetById(id));
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        public void Remove(Guid id)
        {
            var removeCommand = new RemoveOrderCommand(id);
            Bus.SendCommand(removeCommand);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="orderViewModel"></param>
        public void Update(OrderViewModel orderViewModel)
        {
            var updateCommand = _mapper.Map<UpdateOrderCommand>(orderViewModel);
            Bus.SendCommand(updateCommand);
        }

        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }


    }
}
